/*
 * 版权所有 (c) 华为技术有限公司 2022-2022
 */
package com.huawei.cameratest.data;

import android.content.Context;
import android.graphics.ImageFormat;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.util.Size;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.huawei.cameratest.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class StreamView {
    private Spinner resolution;
    private Spinner bitrateMode;
    private Spinner encodeBitrate;
    private Spinner iframeInterval;
    private Spinner peakRatio;
    private Spinner cabacMode;
    private Spinner qualityParamMin;
    private Spinner qualityParamMAX;
    private Spinner requestIFrame;
    private Spinner ltrReq;
    private Spinner dropNextFrame;
    private Spinner svcMode;
    private Spinner format;
    private Spinner protocol;
    private Spinner fps;
    private Context activity;
    public static final Size[] resolutions = new Size[] {
            new Size(3840, 2160),
            new Size(3264, 1836),
            new Size(2560, 1440),
            new Size(1920, 1080),
            new Size(1280,  720),
            new Size( 960,  540),
            new Size( 848,  480),
            new Size( 800,  600),
            new Size( 800,  448),
            new Size( 640,  480),
            new Size( 640,  360),
            new Size( 480,  360),
            new Size( 480,  270),
            new Size( 424,  240),
            new Size( 400,  300),
            new Size( 320,  240),
            new Size( 320,  180),
            new Size( 256,  144),
            new Size( 160,  90),
    };

    public StreamView(Spinner resolution, Spinner encodeBitrate, Spinner iframeInterval,
                      Spinner qualityParamMin, Spinner qualityParamMAX, Spinner requestIFrame,
                      Spinner svcMode, Spinner format, Context activity) {
        this.resolution = resolution;
        this.encodeBitrate = encodeBitrate;
        this.iframeInterval = iframeInterval;
        this.qualityParamMin = qualityParamMin;
        this.qualityParamMAX = qualityParamMAX;
        this.requestIFrame = requestIFrame;
        this.svcMode = svcMode;
        this.format = format;
        this.activity = activity;
    }

    public StreamView(Spinner resolution, Spinner encodeBitrate, Spinner iframeInterval,
                      Spinner qualityParamMin, Spinner qualityParamMAX, Spinner requestIFrame,
                      Spinner ltrReq, Spinner svcMode, Spinner format, Context activity) {
        this.resolution = resolution;
        this.encodeBitrate = encodeBitrate;
        this.iframeInterval = iframeInterval;
        this.qualityParamMin = qualityParamMin;
        this.qualityParamMAX = qualityParamMAX;
        this.requestIFrame = requestIFrame;
        this.ltrReq = ltrReq;
        this.svcMode = svcMode;
        this.format = format;
        this.activity = activity;
    }

    public StreamView(Spinner resolution, Spinner bitrateMode, Spinner encodeBitrate, Spinner iframeInterval, Spinner peakRatio,
                      Spinner cabacMode, Spinner qualityParamMin, Spinner qualityParamMAX, Spinner requestIFrame, Spinner ltrReq,
                      Spinner dropNextFrame, Spinner svcMode, Spinner format, Context activity) {
        this.resolution = resolution;
        this.bitrateMode = bitrateMode;
        this.encodeBitrate = encodeBitrate;
        this.iframeInterval = iframeInterval;
        this.peakRatio = peakRatio;
        this.cabacMode = cabacMode;
        this.qualityParamMin = qualityParamMin;
        this.qualityParamMAX = qualityParamMAX;
        this.requestIFrame = requestIFrame;
        this.ltrReq = ltrReq;
        this.dropNextFrame = dropNextFrame;
        this.svcMode = svcMode;
        this.format = format;
        this.activity = activity;
    }
    public StreamView(Spinner resolution, Spinner encodeBitrate, Spinner qualityParamMin, Spinner qualityParamMAX, Spinner requestIFrame, Spinner protocol,  Spinner fps,Context activity) {
        this.resolution = resolution;
        this.encodeBitrate = encodeBitrate;
        this.qualityParamMin = qualityParamMin;
        this.qualityParamMAX = qualityParamMAX;
        this.requestIFrame = requestIFrame;
        this.protocol = protocol;
        this.fps = fps;
        this.activity = activity;
    }

    public StreamView(Spinner resolution, Spinner encodeBitrate, Spinner qualityParamMin, Spinner qualityParamMAX, Spinner requestIFrame, Spinner fps,Context activity) {
        this.resolution = resolution;
        this.encodeBitrate = encodeBitrate;
        this.qualityParamMin = qualityParamMin;
        this.qualityParamMAX = qualityParamMAX;
        this.requestIFrame = requestIFrame;
        this.fps = fps;
        this.activity = activity;
    }

    public StreamView(Spinner resolution, Spinner encodeBitrate, Spinner qualityParamMin, Spinner qualityParamMAX, Spinner requestIFrame, Context activity) {
        this.resolution = resolution;
        this.encodeBitrate = encodeBitrate;
        this.qualityParamMin = qualityParamMin;
        this.qualityParamMAX = qualityParamMAX;
        this.requestIFrame = requestIFrame;
        this.activity = activity;
    }

//    public StreamView(Spinner resolution, Spinner encodeBitrate, Spinner qualityParamMin, Spinner qualityParamMAX, Spinner requestIFrame, Spinner ltrReq, Spinner dropNextFrame, Context activity) {
//        this.resolution = resolution;
//        this.encodeBitrate = encodeBitrate;
//        this.qualityParamMin = qualityParamMin;
//        this.qualityParamMAX = qualityParamMAX;
//        this.requestIFrame = requestIFrame;
//        this.ltrReq = ltrReq;
//        this.dropNextFrame = dropNextFrame;
//        this.activity = activity;
//    }

    public void initStreamValue(StreamConfigurationMap streamConfigurationMap, int value, Integer[] svcModes) {
        initResolution(streamConfigurationMap, value);
        initEncodeBitrate(value);
        initIframeInterval();
        initQualityParam();
        initRequestIFrame();
        initSvcMode(svcModes);
        initFormat();
    }

    private void initResolution(StreamConfigurationMap streamConfigurationMap, int value) {
//        Size[] resolutions = streamConfigurationMap.getOutputSizes(ImageFormat.JPEG);
        String defaultSize;
        if (value == 0) {
            defaultSize = resolutions[resolutions.length - 1].toString();
        } else {
            defaultSize = resolutions[1].toString();
        }
//        initResolutionWithDefaultValue(resolutions, defaultSize);
        initResolutionWithDefaultValue(defaultSize);
    }

//    public void initResolutionWithDefaultValue(Size[] resolutions, String defaultSize) {
    public void initResolutionWithDefaultValue(String defaultSize) {
        ArrayList strings = new ArrayList();
        int defaultSelection = 0;
        for (int i = 0; i < resolutions.length; i++) {
            Size size = resolutions[i];
            strings.add(size.toString());
            if (defaultSize.equals(size.toString())) {
                defaultSelection = i;
            }
        }
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, strings);
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        resolution.setAdapter(adapter);
        resolution.setSelection(defaultSelection);
    }

    private void initBitrateMode() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("CBR", "ABR", "VBR"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        bitrateMode.setAdapter(adapter);
        bitrateMode.setSelection(0);
    }

    private void initEncodeBitrate(int value) {
        if (value == 0) {
            initEncodeBitrateWithDefaultValue("2048000");
        } else {
            initEncodeBitrateWithDefaultValue("512000");
        }
    }

    public void initEncodeBitrateWithDefaultValue(String defaultValue) {
        int defaultSelection = 0;
        List<String> strings = Arrays.asList("", "64000", "512000", "2048000", "4096000", "8192000");
        for (int i = 0; i < strings.size(); i++) {
            if (strings.get(i).equals(defaultValue)) {
                defaultSelection = i;
            }
        }
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, strings);
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        encodeBitrate.setAdapter(adapter);
        encodeBitrate.setSelection(defaultSelection);
    }

    public void initFps() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("24" ,"", "5", "8", "10", "15", "20", "25", "30"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        fps.setAdapter(adapter);
        fps.setSelection(0);
    }

    public void initProtocol() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("H264", "H265"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        protocol.setAdapter(adapter);
        protocol.setSelection(0);
    }

    // TODO 用txt比较合适 3600是最优值
    private void initIframeInterval() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("1", "1024", "3600", "16384", "65536"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        iframeInterval.setAdapter(adapter);
        iframeInterval.setSelection(2);
    }

    // TODO 用txt比较合适  0是最优值？
    private void initPeakRatio() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("", "0", "5", "10", "30"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        peakRatio.setAdapter(adapter);
        peakRatio.setSelection(0);
    }

    private void initCabacMode() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("", "0", "1"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        cabacMode.setAdapter(adapter);
        cabacMode.setSelection(0);
    }

    // TODO 用txt比较合适
    private void initQualityParam() {
        ArrayAdapter adapterMin = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("", "1", "5", "10", "20", "30", "40", "50"));
        adapterMin.setDropDownViewResource(R.layout.new_spinner_background);
        qualityParamMin.setAdapter(adapterMin);
        qualityParamMin.setSelection(0);

        ArrayAdapter adapterMax = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("", "1", "5", "10", "20", "30", "40", "50"));
        adapterMax.setDropDownViewResource(R.layout.new_spinner_background);
        qualityParamMAX.setAdapter(adapterMax);
        qualityParamMAX.setSelection(0);
    }

    public void initQualityParamWithDefaultValue(String qpMin, String qpMax) {
        int defaultMin = 0;
        int defaultMax = 0;
        List<String> qpRange = Arrays.asList("", "1", "5", "10", "20", "30", "40", "50");
        for (int i = 0; i < qpRange.size(); i++) {
            if (qpRange.get(i).equals(qpMin)) {
                defaultMin = i;
            }
            if (qpRange.get(i).equals(qpMax)) {
                defaultMax = i;
            }
        }
        ArrayAdapter qpMinAdapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, qpRange);
        qpMinAdapter.setDropDownViewResource(R.layout.new_spinner_background);
        qualityParamMin.setAdapter(qpMinAdapter);
        qualityParamMin.setSelection(defaultMin);

        ArrayAdapter qpMaxAdapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, qpRange);
        qpMaxAdapter.setDropDownViewResource(R.layout.new_spinner_background);
        qualityParamMAX.setAdapter(qpMaxAdapter);
        qualityParamMAX.setSelection(defaultMax);
    }

    private void initRequestIFrame() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("", "0", "1"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        requestIFrame.setAdapter(adapter);
        requestIFrame.setSelection(0);
    }

    public void initRequestIFrameWithDefaultValue(String defaultValue) {
        int defaultSelection = 0;
        List<String> strings = Arrays.asList("", "0", "1");
        for (int i = 0; i < strings.size(); i++) {
            if (strings.get(i).equals(defaultValue)) {
                defaultSelection = i;
            }
        }
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, strings);
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        requestIFrame.setAdapter(adapter);
        requestIFrame.setSelection(defaultSelection);
    }

    private void initLtrReq() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("", "0", "1"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        ltrReq.setAdapter(adapter);
        ltrReq.setSelection(0);
    }

    public void initLtrReqWithDefaultValue(String defaultValue) {
        int defaultSelection = 0;
        List<String> strings = Arrays.asList("", "0", "1");
        for (int i = 0; i < strings.size(); i++) {
            if (strings.get(i).equals(defaultValue)) {
                defaultSelection = i;
            }
        }
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("", "0", "1"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        ltrReq.setAdapter(adapter);
        ltrReq.setSelection(defaultSelection);
    }

    private void initDropNextFrame() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("", "0", "1"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        dropNextFrame.setAdapter(adapter);
        dropNextFrame.setSelection(0);
    }

    public void initDropNextFrameDefaultValue(String defaultValue) {
        int defaultSelection = 0;
        List<String> strings = Arrays.asList("", "0", "1");
        for (int i = 0; i < strings.size(); i++) {
            if (strings.get(i).equals(defaultValue)) {
                defaultSelection = i;
            }
        }
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("", "0", "1"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        dropNextFrame.setAdapter(adapter);
        dropNextFrame.setSelection(defaultSelection);
    }

    public void initSvcMode(Integer[] svcModes) {
        ArrayList strings = new ArrayList();
        if (svcModes != null) {
            for (int svc : svcModes) {
                strings.add(svc);
            }
            if (strings.size() > 0) {
                strings.remove(0);
            }
        }
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, strings);
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        svcMode.setAdapter(adapter);
        svcMode.setSelection(0);
    }

    private void initFormat() {
        ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, Arrays.asList("H264", "H265"));
        adapter.setDropDownViewResource(R.layout.new_spinner_background);
        format.setAdapter(adapter);
        format.setSelection(0);
    }

    public Spinner getResolution() {
        return resolution;
    }

    public Spinner getBitrateMode() {
        return bitrateMode;
    }

    public Spinner getEncodeBitrate() {
        return encodeBitrate;
    }

    public Spinner getIframeInterval() {
        return iframeInterval;
    }

    public Spinner getPeakRatio() {
        return peakRatio;
    }

    public Spinner getCabacMode() {
        return cabacMode;
    }

    public Spinner getQualityParamMin() {
        return qualityParamMin;
    }

    public Spinner getQualityParamMAX() {
        return qualityParamMAX;
    }

    public Spinner getRequestIFrame() {
        return requestIFrame;
    }

    public Spinner getLtrReq() {
        return ltrReq;
    }

    public Spinner getDropNextFrame() {
        return dropNextFrame;
    }

    public Spinner getSvcMode() {
        return svcMode;
    }

    public Spinner getFormat() {
        return format;
    }

    public Context getActivity() {
        return activity;
    }
}
